OPTIONAL extension-support.f

{ Behavior of  GLExtension:
   To use words created with this word, use CATCH.
   It will ABORT if the extension was not supported, allowing a fallback plan.
   For ARB extensions, this is probably not that necessary.
}

Requires OpenGL
Requires DLL-tools
Requires A
Requires Tools

Only Forth Definitions

variable lastext
variable lastflag

: supported? ( addr c -- flag )
  GL_EXTENSIONS glGetString zcount 2swap SEARCH nip nip ;

: GLExtension: ( -- <name> <extension> )
  create here >r 0 , lastflag @ , r> lastflag ! 0 parse string,
  does> @ ;

: GLFunction: ( #stack-items -- <name> )
   here lastext @ , lastext !
   here >r 0 , \ address of proc pointer
   >in @ >r
   bl parse string,
   0 r> >in ! CProc:
   last @ name> >body @ >body r> ! ;   \ last word is the cstyle( ) word, so we get the XT out of there

: >extname   cell+ cell+ count ;
: >procname   cell+ cell+ count ; \ 2dup type ;
: >(procaddr)  cell+ @ ; \ dup h. ;

: load-gl-extensions
  lastflag @ begin dup while dup >extname supported? over ! cell+ @ repeat drop
  lastext @ begin dup while dup >procname get-proc-address over >(procaddr) ! @ repeat drop
  ;

\ for development
: verify-gl-extensions
  lastflag @ begin dup while dup >extname supported? 0= if
      dup >extname type s"  not supported" type cr then
    cell+ @ repeat drop
  lastext @ begin dup while dup >procname get-proc-address 0= if
      dup >procname type s"  proc not found" type cr then
  cell+ cell+ @ repeat drop
  ;
